/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cms.dataaccess.hibernate;

import cms.dataaccess.ReviewCategoryDAO;
import cms.exceptions.HibernateAccessLayerException;
import dto.Reviewcategory;
import java.util.List;

/**
 *
 * @author WU DAO BO
 */
public class ReviewCategoryDAOImpl extends  AbstractDAO implements  ReviewCategoryDAO{
    public ReviewCategoryDAOImpl(){
        super();
    }
    @Override
    public Reviewcategory getReviewCategory(String catID) {
         if(catID==null){
            return null;
         }
         try {
            return (Reviewcategory) super.find(Reviewcategory.class,catID);
          } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
        }
          
    }

    @Override
    public List<Reviewcategory> getAllReviewCategories() {
        try{
            return super.findAll(Reviewcategory.class);
        } catch (Throwable t){
               throw new HibernateAccessLayerException(t.toString());
        }
        
    }

  
    @Override
    public boolean updateReviewCategory(String catName, int weight) {
         if(catName==null){
            return false;
         }
         try {
            
           List< Reviewcategory>  catList =  super.findByCustomizeQuery(Reviewcategory.class, 
                   "  catName='"+ catName+"' and weight="+weight);
               Reviewcategory  cat = catList.get(0);
            if(catList.size()<=0 ||cat==null ){
              java.util.UUID uuid  = java.util.UUID.randomUUID();
              cat =  new Reviewcategory(uuid.toString(),catName,weight);
            }
          
             cat.setCatName(catName);
             cat.setWeight(weight);
            
             super.saveOrUpdate(cat);
            
             return true;

        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
        }
    }

    @Override
    public boolean deleteReviewCategory(String catName, int weight) {
        
        
        if(catName==null) {return false;}
        
        
         try{
          Reviewcategory cat = (Reviewcategory) super.findByCustomizeQuery(Reviewcategory.class, " catName='"+catName+"' and weight="+weight).get(0);
          if(cat == null){
              return false;
          }
          super.delete(cat);
          return true;
        } catch (Throwable t){
               throw new HibernateAccessLayerException(t.toString());
        }
    }

    @Override
    public boolean deleteReviewCategory(Reviewcategory reviewCategory) {
       this.delete(reviewCategory);
       return true;
    }
    
     @Override
    public boolean updateReviewCategory(Reviewcategory reviewCategory) {
       this.updateReviewCategory(reviewCategory);
       return true;
    }
    
}
